/**
 * 
 */

LoginPortlet = Class.extend({
	
	init: function()	{
		this.name = "LoginPortlet";
		this.model = {};
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onNeedLogin: function(eventData)	{
		this.currentEventData = eventData;
		//console.log("Need Login");
		//check login?
		var properties = SingletonFactory.getInstance(Application).getSystemProperties();
		var root = properties.get('host.root');
		var loggedIn = properties.get('user.login', undefined);
		var obj = this;
		//no evidence found
		if (loggedIn == undefined)	{
			//query to server
			this.onAjax('ajax', 'check-user-status', {}, 'GET', 
					{'onSuccess': function(ret)	{
						if (ret == 0)	{
							properties.set('user.login', 0, true);
						} else {
							properties.set('user.id', ret.userid, true);
							properties.set('user.name', ret.name, true);
							properties.set('user.login', 1, true);
						}
						obj.recheckLogin(eventData);
					}});
		} else {
			this.recheckLogin(eventData);
		}
	},
	
	recheckLogin: function(eventData)	{
		var subject = SingletonFactory.getInstance(Subject);
		var properties = SingletonFactory.getInstance(Application).getSystemProperties();
		var loggedIn = properties.get('user.login', undefined);
		if (loggedIn == 0)	{
			if (this.currentEventData.disableWarning == true)
				this.model.loginError = '';
			else 
				this.model.loginError = this.getLocalizedText('NeedLogin');
			subject.notifyEvent('ShowPopup', {'id': 'Login',title: '<img src="static/css/beta/images/icon/24/key.png" alt="key" />Đăng nhập', 'content': this.render()});
			$('#LoginPortlet-Username').focus();
		} else {
			subject.notifyEvent('LoginSuccess', eventData);
		}
	},
	
	onPasswordTextBoxKeyUp: function(event)	{
		var keycode = event.keyCode;
		if (keycode == 13)	{
			this.onLoginButtonClick(event);
		}
	},
	
	onLoginButtonClick: function(eventData)	{
		var target = eventData.target;
		var form = $(target).parents('#LoginPortlet-Form:first');
		//ajax request
		var username = $(form).find('#LoginPortlet-Username').val();
		var password = $(form).find('#LoginPortlet-Password').val();
		var remember = $(form).find('#LoginPortlet-Persistent:checked').length;
		var loginError = $(target).parents('#LoginPortlet-Form:first').find('#LoginPortlet-LoginError');
		if (username == '' || password == '')	{
			$(loginError).html(this.getLocalizedText('LoginInsufficientInput'));
		}
		var obj = this;
		this.onAjax('ajax', 'login', {'username':username, 'password':password, 'persistent': remember}, 'POST', 
				{'onSuccess': function(ret)	{
					var properties = SingletonFactory.getInstance(Application).getSystemProperties();
					properties.set('user.id', ret.userid, true);
					properties.set('user.name', ret.name, true);
					properties.set('user.login', 1, true);
					var subject = SingletonFactory.getInstance(Subject);
					subject.notifyEvent('PopupRemove', {id: 'Login'});
					subject.notifyEvent('LoginSuccess', obj.currentEventData);
				},
				'onFailure': function(message)	{
					$(loginError).html(message+'Nếu bạn chắc chắn bạn điền đúng thông tin, vui lòng vào <a onclick="generateEvent(&apos;PopupRemove&apos;, {id: &apos;Login&apos;})" href="#page/RecoverError">đây</a> để xem hướng dẫn');
				}});
	},
	
	run: function()	{
		this.model.loginError = '';
		this.getPortletPlaceholder().drawToCanvas(this.render());
	}
}).implement(RenderInterface).implement(PortletInterface).implement(ObserverInterface).implement(AjaxInterface);

RegisterPortlet = Class.extend({
	init: function()	{
		this.name = "RegisterPortlet";
		this.step = 1;
		this.subject = SingletonFactory.getInstance(Subject);
	},
	
	onBegin: function()	{
		this.model = {};
		this.model.welcomeMsg = this.getLocalizedText('Welcome');
		this.model.nameMsg = this.getLocalizedText('Name');
		this.model.usernameMsg = this.getLocalizedText('Username');
		this.model.passwordMsg = this.getLocalizedText('Password');
		this.model.repasswordMsg = this.getLocalizedText('Repassword');
		this.model.emailMsg = this.getLocalizedText('Email');
		this.model.submitMsg = this.getLocalizedText('Submit');
		this.model.skipMsg = this.getLocalizedText('Skip');
		this.model.registerMsg = this.getLocalizedText('Register');
		this.model.additionInfoMsg = this.getLocalizedText('AdditionInfo');
		this.model.expertiseMsg = this.getLocalizedText('Expertise');
		this.model.tokenMsg = this.getLocalizedText('Token');
		
		this.model.usernameTitle = this.getLocalizedText('UsernameTitle');
		this.model.passwordTitle = this.getLocalizedText('PasswordTitle');
		this.model.repasswordTitle = this.getLocalizedText('RepasswordTitle');
		this.model.emailTitle = this.getLocalizedText('EmailTitle');
		this.registerObserver();
		this.buildTemplate();
	},
	
	buildTemplate: function()	{
		var rootNode = this.requestForResource('ProfileStructure');
		var placeholder = this.requestForResource('ProfileMainContent');
		this.subject.notifyEvent('BuildProfileTemplate', {'id': this.name, 'root':rootNode, 'placeholder': placeholder});
	},
	
	run: function()	{
		this.model.existUsername = "";
		this.model.existEmail = "";
		this.step1();
	},
	
	step1: function()	{
		this.getPortletPlaceholder().drawToCanvas(this.renderView('ViewStep1', this.model));
	},
	
	step1ClearError: function()	{
		this.requestForEffectiveResource('NameError').html('');
		this.requestForEffectiveResource('UsernameError').html('');
		this.requestForEffectiveResource('PasswordError').html('');
		this.requestForEffectiveResource('RepasswordError').html('');
		this.requestForEffectiveResource('EmailError').html('');
		this.requestForEffectiveResource('TokenError').html('');
	},
	
	onWhoIsSelectChanged: function()	{
		var whois = this.requestForEffectiveResource('WhoIs').find('option:selected').val();
		if (whois == 1)	{
			this.requestForEffectiveResource('OptionalArea').show();
		} else {
			this.requestForEffectiveResource('OptionalArea').hide();
		}
	},
	
	onRegisterButtonClick: function(eventData)	{
		this.requestForEffectiveResource('StudentIDError').html('');
		var fullname = this.requestForEffectiveResource('Name').val();
		var username = this.requestForEffectiveResource('Username').val();
		var password = this.requestForEffectiveResource('Password').val();
		var repassword = this.requestForEffectiveResource('Repassword').val();
		var email = this.requestForEffectiveResource('Email').val();
		var token = this.requestForEffectiveResource('Token').val();
		var refererToken = this.getRequest().getParam('token');
		var whois = this.requestForEffectiveResource('WhoIs').find('option:selected').val();
		var studentID = this.requestForEffectiveResource('StudentID').val();
		if (studentID.length != 8 && studentID.length != 0)	{
			this.requestForEffectiveResource('StudentIDError').html('Bạn nhập không đúng mã số sinh viên.');
			return;
		}
		
		var subject = SingletonFactory.getInstance(Subject);
		
		var obj = this;
		this.step1ClearError();
		//console.log('register button click');
		this.onAjax('ajax', 'register', 
				{'username':username, 'password':password, 'repassword':repassword, 'email':email, 'fullname':fullname
					, 'token': token, 'refererToken': refererToken, 'whois': whois, 'sid': studentID}
				, 'POST', 
				{onSuccess: function(ret)	{
					subject.notifyEvent('UpdateUserStatus');
					obj.profile_id = ret.profile_id;
					obj.step3();
				},
				onFailure: function(message)	{
					for(var i in message)	{
						var err = message[i];
						obj.handleRegisterError(err.type, err.msg);
					}
				}});
	},
	
	handleRegisterError: function(errType, errMsg)	{
		switch(errType)	{
		case "fullname":
			this.requestForEffectiveResource('NameError').html(errMsg);
			break;
		case "username":
			this.requestForEffectiveResource('UsernameError').html(errMsg);
			break;
		case "password":
			this.requestForEffectiveResource('PasswordError').html(errMsg);
			break;
		case "repassword":
			this.requestForEffectiveResource('RepasswordError').html(errMsg);
			break;
		case "email":
			this.requestForEffectiveResource('EmailError').html(errMsg);
			break;
		case "token":
			this.requestForEffectiveResource('TokenError').html(errMsg);
			break;
		}
	},
	
	onLoginSuccess: function(eventData)	{
		if (eventData.type == 'Register')	{
			this.end();
		}
	},
	
	step3: function()	{
		this.getPortletPlaceholder().paintCanvas(this.renderView('ViewStep3', this.model));
		var subject = SingletonFactory.getInstance(Subject);
		subject.notifyEvent('RenderExpertiseEditor', {placeholder:this.requestForEffectiveResource('ExpertisePlaceholder'), namespace:'RegisterPortlet'});
		subject.notifyEvent('RenderInterestEditor', {placeholder:this.requestForEffectiveResource('InterestPlaceholder'), namespace:'RegisterPortlet'});
	},
	
	step2: function()	{
		this.getPortletPlaceholder().paintCanvas(this.renderView('ViewStep2', this.model));
		this.requestForEffectiveResource('ProfileEdit').html(this.requestForResource('ViewData').html());
		this.subject.notifyEvent('DisplayRemainingNodes', {'id': this.name});
		this.subject.notifyEvent('NeedFormatting');
		this.subject.notifyEvent("ProfileInputBindEvent");
	},
	
	onSkipExpertiseRegisterButtonClick: function(eventData)	{
		this.end();
	},
	
	onSubmitExpertiseRegisterButtonClick: function(eventData)	{
		var subject = SingletonFactory.getInstance(Subject);
		var obj = this;
		this.submitComplete = 0;
		subject.notifyEvent('SubmitExpertiseForward', {callback: function()	{
			obj.expertiseSubmitted();
		}, namespace: this.name});
		subject.notifyEvent('SubmitInterestForward', {callback: function()	{
			obj.expertiseSubmitted();
		}, namespace: this.name});
	},
	
	expertiseSubmitted: function()	{
		this.submitComplete++;
		if (this.submitComplete >= 2)	{
			this.end();
		}
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	end: function()	{
		var request = new Request("Home");
		var subject = SingletonFactory.getInstance(Subject);
		subject.notifyEvent('RequestRoute', request);
	},
	
	onSkipEditRegisterButtonClick: function(eventData)	{
		this.step3();
	},
	
	onSubmitEditRegisterButtonClick: function(eventData)	{
		var root = this.requestForEffectiveResource('ProfileEdit');
		var subject = SingletonFactory.getInstance(Subject);
		var obj = this;
		obj.requestForEffectiveResource('RegisterEditSuccess').html('');
		obj.requestForEffectiveResource('RegisterEditError').html('');
		subject.notifyEvent('SaveProfile', {root: root, 
			success: function() {
				obj.step3();
			},
			failure: function()	{
				obj.step3();
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(ObserverInterface).implement(AjaxInterface);
